//requires:
//	rendertext.js (only for the string manip function getTarget())
//	nav.js (for the gNavPath variable)
	
function handleClicks(e)
{
	if (e.target)
	{
		if (e.target.nodeName == "a" || e.target.nodeName == "A")
		{	
			//e.target.href is a full path, not a relative one	
		    var href = e.target.href;
			if (isPartOfBook(href, gBookDirectory))
			{
			    e.preventDefault();
			    e.stopPropagation();
			    //make it a relative path
    		    var idx = href.indexOf(gBookDirectory);
                if (idx != -1)
                {
                    var first_after_relative = idx + gBookDirectory.length;
                    href= href.substring(first_after_relative, href.length);
                    //trim the first slash if there is one
                    if (href[0] == "/") href = href.substring(1, href.length);
                }
			    book.loadSmilUrl(href);
			}
			//this link is not part of the book
			else
			{
				//TODO we should actually open this document in a new tab

			}	
			
		}
	}
}

function isPartOfBook(address, bookDir)
{
 	//see if the parent path of the navigation file is also in the given path
	//theoretically, the parent path of the navigation file is the root dir of the book
	if (address.indexOf(bookDir) != -1) return true;
	else return false;
}

//where path = blah://rel/a/tive/pa/th.ext
//and relative = /rel/a/tive/
//leave only pa/th.ext
function doRelativePathMagic(path, relative)
{
    var idx = path.indexOf(relative);
    if (idx != -1)
    {
        var first_after_relative = idx + relative.length;
        return path.substring(first_after_relative, path.length);
    }
    return path;
}